import { NativeModules, Platform } from 'react-native';

const { WeChatManager } = NativeModules;

class WeChat {
  /**
   * 注册微信应用
   * @param {string} appId - 微信应用ID
   * @returns {Promise<boolean>}
   */
  static registerApp(appId) {
    if (!WeChatManager) {
      return Promise.reject(new Error('WeChatManager is not available'));
    }
    
    return WeChatManager.registerApp(appId);
  }

  /**
   * 微信登录
   * @param {string} appId - 微信应用ID
   * @returns {Promise<any>}
   */
  static login(appId) {
    if (!WeChatManager) {
      return Promise.reject(new Error('WeChatManager is not available'));
    }
    
    return WeChatManager.login(appId);
  }

  /**
   * 微信支付
   * @param {Object} payload - 支付参数
   * @param {string} payload.appId - 应用ID
   * @param {string} payload.partnerId - 商户号
   * @param {string} payload.prepayId - 预支付订单ID
   * @param {string} payload.nonceStr - 随机字符串
   * @param {string} payload.timeStamp - 时间戳
   * @param {string} payload.sign - 签名
   * @returns {Promise<any>}
   */
  static pay(payload) {
    if (!WeChatManager) {
      return Promise.reject(new Error('WeChatManager is not available'));
    }
    
    return WeChatManager.pay(payload);
  }

  /**
   * 检查微信是否已安装
   * @returns {Promise<boolean>}
   */
  static isWXAppInstalled() {
    if (!WeChatManager) {
      // 在iOS模拟器上可能不可用
      return Promise.resolve(Platform.OS === 'ios' ? true : false);
    }
    
    return WeChatManager.isWXAppInstalled();
  }
}

export default WeChat;